﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DataLibrary;
using Common;

namespace TicketSystem
{
    public partial class UC_Operators : UserControl
    {
        public UC_Operators()
        {
            InitializeComponent();
        }
        private TicketSystemEntities tse = new TicketSystemEntities();
        PublicMethod pm = new PublicMethod();
        private void UC_Operators_Load(object sender, EventArgs e)
        {
            loaddata();
        }
        private void loaddata()
        {
            var ope = tse.Operators.Where(u => true).Select(u => new { u.Name, u.Roles.RoleName, u.IDCard, u.Phone, u.Address, u.OperatorID }).ToList();
            this.dataGridView1.DataSource = ope;
            this.dataGridView1.Columns[0].HeaderText = "操作员姓名";
            this.dataGridView1.Columns[1].HeaderText = "所属权限";
            this.dataGridView1.Columns[2].HeaderText = "身份证号";
            this.dataGridView1.Columns[3].HeaderText = "手机号";
            this.dataGridView1.Columns[4].HeaderText = "住址";
            this.dataGridView1.Columns[5].HeaderText = "id";
            //int roleid = tse.Operators.Where(u => u.OperatorID == 1).FirstOrDefault().RoleID;
            int roleid = tse.Operators.Where(u => u.OperatorID == GetOperator.OperatorID).FirstOrDefault().RoleID;
            List<string> l = new List<string>();
            var right = tse.RoleRights.Where(u => u.RoieID == roleid);
            foreach (var r in right)
            {
                l.Add(r.Rights.RightName);
            }
            string name = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            //验证操作员是否可编辑操作员
            pm.CanMotifyOperator(this.btnAdd, this.btnUpdata, this.btnDelete, l, name);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (Form_OperatorAdd oa = new Form_OperatorAdd())
            {
                oa.ShowDialog();
                //这里调用绑定函数
                loaddata();
            }
        }

        private void btnUpdata_Click(object sender, EventArgs e)
        {
            int rows = this.dataGridView1.SelectedRows.Count;
            if (rows > 1)
            {
                MessageBox.Show("一次只能修改一行");
            }
            else if (rows < 1)
            {
                MessageBox.Show("你当前没有选中任何行");
            }
            else
            {
                using (Form_OperatorUpdate ou = new Form_OperatorUpdate())
                {
                    ou.OperatorName = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
                    ou.ShowDialog();
                    //这里调用绑定函数
                    loaddata();
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult dresult = MessageBox.Show("是否要删除该操作员", "删除操作员", MessageBoxButtons.YesNo);
            if (dresult == DialogResult.Yes)
            {
                var count = this.dataGridView1.SelectedRows.Count;
                if (count > 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        //string ope = this.dataGridView1.SelectedRows[i].Cells[0].Value.ToString();
                        int opeid = Convert.ToInt32(this.dataGridView1.SelectedRows[i].Cells[5].Value.ToString());
                        var operators = tse.Operators.Where(u => u.OperatorID == opeid);
                        var sales = tse.Sales.Where(u => u.OperatorID == opeid);
                        if (sales != null && sales.Count() > 0)
                        {
                            MessageBox.Show("该操作员已经开过票，不能删除");
                            return;
                        }
                        tse.Operators.RemoveRange(operators);
                    }
                    int result = tse.SaveChanges();
                    if (result > 0)
                    {
                        MessageBox.Show("删除成功");
                        loaddata();
                    }
                    else
                    {
                        MessageBox.Show("删除失败了！请稍后重试");
                    }
                }
                else
                {
                    MessageBox.Show("你当前没有选中任何行！");
                }
            }            
        }

        /// <summary>
        /// 没有显示电话权限的处理
        /// </summary>
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            var ope = tse.Operators.Where(u => true).Select(u => new { u.Name, u.Roles.RoleName, u.IDCard, u.Phone, u.Address }).ToList();

            int roleid = tse.Operators.Where(u => u.OperatorID == GetOperator.OperatorID).FirstOrDefault().RoleID;
            List<string> l = new List<string>();
            var right = tse.RoleRights.Where(u => u.RoieID == roleid).ToList();
            foreach (var r in right)
            {
                l.Add(r.Rights.RightName);
            }
            //格式化单元格的列
            if (e.ColumnIndex == dataGridView1.Columns[3].Index)
            {
                if (pm.DisplayPhone(l) == false)
                {
                    for (var i = 0; i < this.dataGridView1.RowCount; i++)
                    {
                        //格式化单元格的行
                        if (e.RowIndex == i)
                        {
                            if (ope[i].Phone != null && ope[i].Phone.Length==11)
                            e.Value = ope[i].Phone.Substring(0, 3) + "********";
                        }
                    }
                }
            }
        }
    }
}
